package com.threadmethod.AtomicLong;

import java.util.concurrent.atomic.AtomicLong;

/**
 * @PackageName:com.threadmethod.AtomicLong
 * @ClassName:Indicator
 * @Description: 使用原子变量定义一个计数器 该计数器在整个程序中都能使用
 *               并且所有的地方都使用这一个计数器，这个计数器可以设计为单例
 * @Author: zhangxiaoyu
 * @date 2020/12/10 17:42
 */
public class Indicator {

    //构造方法私有化
    private  Indicator(){}

    //定义一个私有的本类静态的对象
    private static final  Indicator INSTANCE = new Indicator();

    //提供一个公共静态方法返回该类唯一实例
    public static  Indicator getInstance(){
        return  INSTANCE;
    }

    //使用原子变量类保存请求总数，成功数，失败数
    //记录请求总数
    private final AtomicLong requestCount = new AtomicLong(0);
    private final AtomicLong successCount = new AtomicLong(0);
    private final AtomicLong fiaLureCount = new AtomicLong(0);

    //有新的请求
    public void newRequestReceive(){
        requestCount.incrementAndGet();
    }

    //处理成功
    public void  requestProcessSuccess(){
        successCount.incrementAndGet();
    }

    //处理失败
    public void  requestProcessFailure(){
        fiaLureCount.incrementAndGet();
    }

    //查看总数，成功数，失败数
    public long getRequestCount(){
        return requestCount.get();
    }
    public long getSuccessRequestCount(){
        return successCount.get();
    }
    public long getFailRequestCount(){
        return fiaLureCount.get();
    }

}
